package com.lc.ibps.web.restConf;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.base.core.util.string.StringUtil;
import com.lc.ibps.cloud.constants.ParameterKey;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket buildDocket(){
    	
    	String isOpenToken = AppUtil.getProperty("webapi.isOpenToken");

        List<Parameter> pars = new ArrayList<Parameter>();
    	// 增加请求头account用于测试
    	if(StringUtil.isEmpty(isOpenToken)||!Boolean.valueOf(isOpenToken)){
	    	ParameterBuilder accountPar = new ParameterBuilder();
	        accountPar.name(ParameterKey.HEADER_AUTHORIZATION).description("申请的token")
	    	.modelRef(new ModelRef("string")).parameterType("header") 
	    	.required(false).build(); 
	    	pars.add(accountPar.build());
    	}
    	
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInf())
                .select().apis(RequestHandlerSelectors.basePackage("com.lc.ibps.platform"))//controller路径
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    private ApiInfo buildApiInf(){
        return new ApiInfoBuilder()
                .title("IBPS接口")
                .termsOfServiceUrl("http://www.bpmhome.cn")
                .description("IBPS接口")
                .contact(new Contact("广州流辰信息技术有限公司", "http://www.bpmhome.cn", "admin@bpmhome.cn"))
                .build();

    }
}
